package com.tywj.buscustomerapp.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.CheckFormatUtil;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.head_title)
    TextView headtitel;
    @BindView(R.id.register_clear)
    ImageView clear;
    @BindView(R.id.choose_number)
    TextView chooseContry;
    @BindView(R.id.register_edit)
    EditText editText;
    @BindView(R.id.register_submit)
    TextView submit;
    @BindView(R.id.register_call)
    TextView callPc;
    @BindView(R.id.show_pact)
    TextView useInfo;
    private boolean isCanClick = false;

    @Override
    protected int getContentId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        headtitel.setText("手机注册");
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCanClick){
                    Intent intent = new Intent(RegisterActivity.this,RegisterPhoneCodeActivity.class);
                    intent.putExtra("phone",editText.getText().toString());
                    startActivity(intent);
                    finish();
                }
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = editText.getText().toString();
                if (!phone.equals("")) {
                    editText.setText("");
                }
            }
        });

        chooseContry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastJoe.getmToastJoe().ToastShow(RegisterActivity.this, null, "目前仅支持中国大陆手机号");
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phone = editText.getText().toString();
                isCanClick = CheckFormatUtil.checkPhone(phone);
                if (!phone.equals("")) {
                    clear.setVisibility(View.VISIBLE);
                } else {
                    clear.setVisibility(View.GONE);
                }
                if (isCanClick) {
                    submit.setBackgroundResource(R.drawable.bg_ori_shape1);
                    submit.setTextColor(getResources().getColor(R.color.text));
                } else {
                    submit.setBackgroundResource(R.drawable.bg_gry_shape11);
                    submit.setTextColor(getResources().getColor(R.color.text_help));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        callPc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final AlertDialog build = new AlertDialog.Builder(RegisterActivity.this,R.style.AlertDialogStyle).create();
                //自定义布局
                View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
                build.setView(view, 0, 0, 0, 0);
                build.show();
                //得到当前显示设备的宽度，单位是像素
                int width = getWindowManager().getDefaultDisplay().getWidth();
                //得到这个dialog界面的参数对象
                WindowManager.LayoutParams params = build.getWindow().getAttributes();
                //设置dialog的界面宽度
                params.width = width-(width/3);
                //设置dialog高度为包裹内容
                params.height =  WindowManager.LayoutParams.WRAP_CONTENT;
                //设置dialog的重心
                params.gravity = Gravity.CENTER;
                build.getWindow().setAttributes(params);
                TextView titleTv = view.findViewById(R.id.message);
                TextView messageTv = view.findViewById(R.id.message1);
                titleTv.setText("拨打客服电话");
                final String number = Config.CALL_NUMBER;
                messageTv.setText(number);
                Button btn_sure = view.findViewById(R.id.yes);
                Button btn_cancel = view.findViewById(R.id.no);
                btn_sure.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+number));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        build.dismiss();
                    }
                });
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        build.dismiss();
                    }
                });
            }
        });

        useInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,InfoActivity.class);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("注册1");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("注册1");
        MobclickAgent.onPause(this);
    }
}
